﻿using System;
using YEA.Infrastructure.Gateway;
using YEA.Monitor.LogService.States;
using YEA.Utilities.Service.Controller;

namespace YEA.Monitor.LogService
{
    public class ServiceStateController : IDisposable
    {
        protected internal RunningState runningState;
        protected internal DiagnosticState diagnosticState;
        protected internal RecoveryState recoveryState;

        protected internal IServiceState state;
        private bool disposed;


        internal ServiceStateController()
        {
            var logger = LogManager.Logger.Get("YEA.Monitor.LogService");
            runningState = new RunningState() { Logger =  logger};
            diagnosticState = new DiagnosticState() {Logger = logger};
            recoveryState = new RecoveryState(runningState) {Logger = logger};
            diagnosticState.Logger = runningState.Logger;
            ServiceStateBase.StatusChanged += ServiceStateBase_StatusChanged;
            state = runningState;
            disposed = false;
        }
        public ServiceStateController(IGatewayFactory gf)
            : this()
        {
            runningState.GatewayFactory = gf;
        }
        ~ServiceStateController()
        {
            Dispose(false);
        }

        internal void ServiceStateBase_StatusChanged(object sender, EventArgs<StateEventArgs> e)
        {
            switch (e.EventData.state)
            {
                case StateEvent.Errored: ChangeState(diagnosticState);
                    diagnosticState.SetException((Exception) e.EventData.stateDetails);
                    state.Start();
                    break;
                case StateEvent.Diagnosed: ChangeState(recoveryState);
                    recoveryState.Diagnosis = (Diagnostic) e.EventData.stateDetails;
                    state.Start();
                    break;
                case StateEvent.Recovered: ChangeState(runningState);
                    state.Start();
                    break;
                case StateEvent.Stopped :
                    ChangeState(runningState);
                    state.Stop();
                    //doesn't call the start automatically, waits for user to do so.
                    break;
               
            }
        }

        internal virtual void ChangeState(IServiceState newState)
        {
            state = newState;
        }

        public virtual void Start()
        {
            state.Start();
        }

        public virtual void Stop()
        {
            state.Stop();
        }

        public virtual void Pause()
        {
            state.Pause();
        }

        public virtual void Continue()
        {
            state.Continue();
        }


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        internal void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if( runningState != null)
                    runningState.Dispose();
                if(diagnosticState != null)
                    diagnosticState.Dispose();
                if(recoveryState != null)
                    recoveryState.Dispose();
            }
            disposed = true;
        }
    }
}
